﻿@using Apps.Common;
@using Apps.Web.Core;
@using Apps.Models.Sys;
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Index_Layout.cshtml";
    List<PermModel> perm = ViewBag.perm;
    if ( perm == null )
    {
        perm = new List<PermModel>();
    }
}
<script src="@Url.Content("~/Scripts/jquery.easyui.plus.js")" type="text/javascript"></script>
<div class="mvctool">
    @Html.ToolButton("btnSave","icon-save","保存",true,perm,"Save")
</div>
<table style="width: 100%">
    <tbody>
        <tr>
            <td style="width: 420px; padding-right: 3px; vertical-align: top">
                <table id="roleList"></table>
            </td>
            <td style="width: 200px; padding-right: 3px; vertical-align: top">
                <table id="moduleList"></table>
            </td>
            <td>
                <table id="operateList"></table>
            </td>
        </tr>
    </tbody>
</table>
<script type="text/javascript">
    var curModuleId, curRoleId, curModuleName, curRoleName;
    curModuleName = "?";
    curRoleName = "?";

    $('#roleList').datagrid({
        url: '@Url.Action("GetRoleList")',
        width: 420,
        height: $(window).height() - 35,
        method: 'post',
        fitColumns: true,
        sortName: 'CreateTime',
        sortOrder: 'desc',
        idField: 'Id',
        pageSize: 10,
        pageList: [10, 20, 30, 40, 50],
        pagination: true,
        stripted: true,
        singleSelect: true,
        title: "角色列表",
        columns: [[
            { field: 'Id', title: 'ID', width: 80, hidden: true },
            { field: 'Name', title: '角色组', width: 80, sortable: true },
            { field: 'Description', title: '说明', width: 80, sortable: true },
            { field: 'CreateTime', title: '创建时间', width: 80, sortable: true },
            { field: 'CreatePerson', title: '创建人', width: 80, sortable: true, hidden: true }
        ]],
        onClickRow: function (index, data) {
            var row = $('#roleList').datagrid('getSelected');
            if (row != null) {
                curRoleId = row.Id;
                curRoleName = row.Name;
                $('#operateList').datagrid({ url: "/SysRight/GetRightByRoleAndModule?roleId=" + curRoleId + "&moduleId=" + curModuleId });
                $('#operateList').datagrid({ title: '角色组: ' + curRoleName + '模块：' + curModuleName });

            }
        }
    });

    $('#moduleList').treegrid({
        url: '@Url.Action("GetModuleList")',
        width: 300,
        height: $(window).height() - 35,
        method: 'post',
        fitColumns: true,
        treeField: 'Name',
        idField: 'Id',
        pagination: true,
        stripted: true,
        singleSelect: true,
        title: '模块列表',
        columns: [[
            { field: 'Id', title: '唯一标识', width: 120, hidden: true },
            { field: 'Name', title: '名称', width: 220, sortable: true },
            { field: 'EnglishName', title: '英文名称', width: 80, sortable: true, hidden: true },
            { field: 'ParentId', title: '上级Id', width: 80, sortable: true, hidden: true },
            { field: 'Url', title: '链接地址', width: 80, sortable: true, hidden: true },
            { field: 'Iconic', title: '图标', width: 80, sortable: true, hidden: true },
            { field: 'Sort', title: '排序号', width: 80, sortable: true, hidden: true },
            { field: 'Remark', title: '说明', width: 80, sortable: true, hidden: true },
            {
                field: 'Enable', title: '是否启用', width: 60, align: 'center',hidden:true, formatter: function (value) {
                    if (value) {
                        return "<img src='/Content/Images/icon/pass.png' />";
                    } else {
                        return "<img src='/Content/Images/icon/no.png' />";
                    }
                }
            },
            { field: 'CreatePerson', title: '创建人', width: 80, sortable: true, hidden: true },
            { field: 'CreateTime', title: '创建时间', width: 120, sortable: true, hidden: true },
            {
                field: 'IsLast', title: '是否最后一项', width: 100, align: 'center', hidden: true, formatter: function (value) {
                    if (value) {
                        return "是";
                    } else {
                        return "否";
                    }
                }
            }
        ]],
        onClickRow: function (index, data) {
            var row = $('#moduleList').treegrid('getSelected');
            if (row != null) {
                curModuleId = row.Id;
                curModuleName = row.Name;
                if (curRoleId == null && row.IsLast) {
                    $.messageBox5s('提示', "请再选择一个角色！");
                    return;
                }
                $('#operateList').datagrid({ url: '/SysRight/GetRightByRoleAndModule?roleId=' + curRoleId + "&moduleId=" + curModuleId });
                $('#operateList').datagrid({ title: '角色组：' + curRoleName + ">>模块：" + (row.IsLast ? curModuleName:"[请在选择最后菜单项]") });
            }
        }
    });

    $('#operateList').datagrid({
        url: '@Url.Action("GetRightByRoleAndModule")',
        width: $(window).width() - 736,
        method: 'post',
        height: $(window).height() - 35,
        fitColumns: true,
        sortName: 'CreateTime',
        sortOrder: 'desc',
        idField: 'Id',
        striped: true, //奇偶行是否区分
        singleSelect: true,//单选模式
        title: '授权操作',
        columns: [[
            { field: 'Id', title: 'ID', width: 80, hidden: true },
            { field: 'Name', title: '名称', width: 80, sortable: true },
            { field: 'KeyCode', title: '操作码', width: 80, sortable: true },
            {
                field: 'isvalid', title: "<a href='#' title='@Suggestion.Select' onclick=\"SelAll();\" ><img src='/Content/Images/icon/select.gif'/></a><a href='#' title='@Suggestion.UnSelect' onclick=\"UnSelAll();\" ><img src='/Content/Images/icon/unselect.gif'/></a>", align: 'center', width: 30, formatter: function (value) {
                    if (value) {
                        return "<input type='checkbox' checked='checked' value=" + value + "/>";
                    } else {
                        return "<input type='checkbox' value=" + value + "/>";
                    }
                }
            },
            { field: 'RightId', title: '模块ID', width: 80, hidden: true }
        ]]
    });


    function SelAll() {
        $("td[field='isvalid'] input").prop("checked", true);
       // $("#btnSave").trigger("click");
        return;
    }
    function UnSelAll() {
        $("td[field='isvalid'] input").prop("checked", false);
      //  $("#btnSave").trigger("click");
        return;
    }

    $('#btnSave').click(function () {
        var updateRows = 0;
        //获取当前页所有行
        var rows = $('#operateList').datagrid("getRows");

        for (var i = 0; i < rows.length; i++) {
            var setFlag = $("td[field='isvalid'] input").eq(i).prop("checked");
            if (setFlag != rows[i].isvalid) {   //判断是否有做修改
                $.ajax({
                    url: '@Url.Action("UpdateRight")',
                    type: "Post",
                    data: { "Id": rows[i].Id, "RightId": rows[i].RightId, "KeyCode": rows[i].KeyCode, "IsValid": setFlag },
                    async: false,
                    dataType: 'json'
                });
                alert('1次');
                updateRows++;
            }
        }
        if (updateRows > 0) {
            $.messageBox5s('提示', '保存成功！');
        } else {
            $.messageBox5s('提示', '@Suggestion.NoAnyChanges！');
        }

    });

</script>























